<?php
	session_start();
	require_once("../../db/dbclass.php");
	require_once("../../model/userinfo.php");
	require_once("../../utils/common.php");
	require_once("../../utils/config.php");
	$myDb=&DbController::getInstance();
	// $type=$_POST['user_type'];
	$username="";
	if(isset($_SESSION['name'])){
		$username=$_SESSION['name'];
	}
	// echo $username;
	$message=$_POST['message'];
	$issue_date=$_POST['issue_date']." ".$_POST['issue_h'];
	$message_obj=$_POST['message_obj'];
	$message_type=$_POST['message_type'];
	str_replace("/","-",$issue_date);
	$param="SELECT * FROM yj_user_info where user_name='".$username."'";
	$result=$myDb->exec_query($param);
	$user_power="";
	$user_id="";
	if($result==TRUE){
		while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
			$user_power=$row['user_power'];
			$user_id=$row['user_id'];
		}
	}
	switch ($message_type) {
		case '1':
			//私信的形式推送
			switch($message_obj){
				case '1':
				// 全部对象
					$param="INSERT INTO yj_primsg (sender_id,receiver_id,primsg_content,primsg_time) VALUES ('".$user_id."',4,'".$message."','".$issue_date."')";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						$param="select * from yj_primsg where sender_id='".$user_id."' AND primsg_time='".$issue_date."' AND primsg_content='".$message."'";
						$result=$myDb->exec_query($param);
						if($result==TRUE){
							$arrayName=array();
							while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
								$arrayName = array('primsg_id' =>$row['primsg_id'],
								'primsg_time'=>$row['primsg_time'],
								'primsg_content'=>$row['primsg_content'],
								'message_obj'=>'全部对象',
								'message_type'=>'私信');
							}
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}else{
							$arrayName = array('primsg_id' => "",
								'primsg_time'=>$issue_date,
								'primsg_content'=>$message,
								'message_obj'=>'全部对象',
								'message_type'=>'私信');
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}
						// return;
					}else{
						echo "{'result':'2'}";
					}					
					break;
				// 开发者
				case '2':
					$param="INSERT INTO yj_primsg (sender_id,receiver_id,primsg_content,primsg_time) VALUES ('".$user_id."',5,'".$message."','".$issue_date."')";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						$param="select * from yj_primsg where sender_id='".$user_id."' AND primsg_time='".$issue_date."' AND primsg_content='".$message."'";
						$result=$myDb->exec_query($param);
						if($result==TRUE){
							$arrayName=array();
							while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
								$arrayName = array('primsg_id' =>$row['primsg_id'],
								'primsg_time'=>$row['primsg_time'],
								'primsg_content'=>$row['primsg_content'],
								'message_obj'=>'开发者',
								'message_type'=>'私信');
							}
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}else{
							$arrayName = array('primsg_id' => "",
								'primsg_time'=>$issue_date,
								'primsg_content'=>$message,
								'message_obj'=>'开发者',
								'message_type'=>'私信');
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}
						// return;
					}else{
						echo "{'result':'2'}";
					}					
					break;
			}
			break;
		case '2':
			//动态的形式推送
			$myImg=new UpdateUserInfo();
			getFile("uploadedfile",$user_id,$myImg,2);

			switch ($message_obj) {
				case '1':
				// 全部对象
					// $array_imgs=explode(";",$myImg->img_url);
					// $imgs_str="";
					// foreach ($array_imgs as $value) {
					// 	if($value==""){
					// 		continue;
					// 	}else{
					// 		$imgs_str+=URL_ADMIN.substr($value,2);
					// 		$imgs_str+=";";
					// 	}
					// }
					$param="INSERT INTO yj_tweets (user_id,tweets_content,tweets_time,tweets_img,send_range) VALUES ('".$user_id."','".$message."','".$issue_date."','".$myImg->img_url."',1)";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						$param="select * from yj_tweets where user_id='".$user_id."' AND tweets_time='".$issue_date."' AND tweets_content='".$message."'";
						$result=$myDb->exec_query($param);
						if($result==TRUE){
							$arrayName=array();
							while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
								$arrayName = array('primsg_id' =>$row['tweets_id'],
								'primsg_time'=>$row['tweets_time'],
								'primsg_content'=>$row['tweets_content'],
								'message_obj'=>'全部对象',
								'message_imgs'=>URL_ADMIN.substr($myImg->img_url,2),
								'message_type'=>'动态');
							}
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}else{
							$arrayName = array('primsg_id' => "",
								'primsg_time'=>$issue_date,
								'primsg_content'=>$message,
								'message_obj'=>'全部对象',
								'message_imgs'=>URL_ADMIN.substr($myImg->img_url,2),
								'message_type'=>'动态');
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}
					}					
					break;
				// 开发者
				case '2':
					$param="INSERT INTO yj_tweets (user_id,tweets_content,tweets_time,tweets_img,send_range) VALUES ('".$user_id."','".$message."','".$issue_date."','".$myImg->img_url."',2)";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						$param="select * from yj_tweets where user_id='".$user_id."' AND tweets_time='".$issue_date."' AND tweets_content='".$message."'";
						$result=$myDb->exec_query($param);
						if($result==TRUE){
							$arrayName=array();
							while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
								$arrayName = array('primsg_id' =>$row['tweets_id'],
								'primsg_time'=>$row['tweets_time'],
								'primsg_content'=>$row['tweets_content'],
								'message_obj'=>'开发者',
								'message_imgs'=>URL_ADMIN.substr($myImg->img_url,2),
								'message_type'=>'动态');
							}
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}else{
							$arrayName = array('primsg_id' => "",
								'primsg_time'=>$issue_date,
								'primsg_content'=>$message,
								'message_obj'=>'开发者',
								'message_imgs'=>URL_ADMIN.substr($myImg->img_url,2),
								'message_type'=>'动态');
							$arr= array('result' => 1,
							"primsg" =>$arrayName);
							echo json_encode($arr);
						}
					}					
					break;
				default:
					break;
			}
		default:
			break;
	}
?>